import axios from 'axios'
import { Message } from 'element-ui';
import router from '../router'

// 封装响应信息
axios.interceptors.response.use(success=>{

    if(success.status==200){
        if(success.data.code==510 || success.data.code==500 || success.data.code==401 || success.data.code==403 || success.data.code==1000 || success.data.code==1001 || success.data.code==1002){

            Message.error({message:success.data.message});
            router.replace("/")
        }else{
            console.log(JSON.stringify(success.data))
            if(success.data.message.length>0){
            Message.info({message:success.data.message})
            }
        }
    }

    return success.data;
},error=>{

    if(error.data!=undefined){
    if(error.data.code==510){

        router.replace("/")
    }
    }
        Message.error({message:'服务器出错了！'})
        router.replace("/")
})
// 请求拦截
axios.interceptors.request.use(config=>{

    if(window.sessionStorage.getItem("token")){
        // 如果存在请求携带

        config.headers['Authorization'] = window.sessionStorage.getItem("token");
    }
    return config
},error => {
    console.log(error)
})
let baseUrl=''
console.log(baseUrl)
export const postRequest=(url,params)=>{
   return axios({
       method:'POST',
       url:url,
       data:params
   })
}
export const getRequest=(url,params)=>{
    return axios({
        method:'GET',
        url:url,
        data:params
    })
}
export const deleteRequest=(url,params)=>{
    return axios({
        method:'DELETE',
        url:url,
        data:params
    })
}